<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
    private $_id;

    /**
     * Autentica o usuário.
     * 
     * @return boolean.
     */
    public function authenticate()
    {
        $model = Usuario::model()->findByAttributes(array('usuario' => $this->username));
        
        if ($model === null) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } else if (!Usuario::verifySenha($this->password, $model->senha)) {
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        } else {
            $this->_id = $model->id;
            $this->errorCode = self::ERROR_NONE;
        }
        
        return !$this->errorCode;
    }
    
    public function getId()
    {
        return $this->_id;
    }

}
